package com.lzm.ydpt.module.mall.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.byt.framlib.base.BaseFragment;
import com.byt.framlib.baseadapter.rv.RvCommonAdapter;
import com.byt.framlib.baseadapter.rv.RvViewHolder;
import com.byt.framlib.basemvp.basebean.ListPageBean;
import com.byt.framlib.basemvp.boby.FormBodys;
import com.byt.framlib.commonutils.image.ImageLoaderUtils;
import com.byt.framlib.commonwidget.LoadingTip;
import com.byt.framlib.commonwidget.OnNoDoubleClickListener;
import com.byt.framlib.commonwidget.RoundedConnerImageView;
import com.byt.framlib.commonwidget.refresh.RefreshHeaderView;
import com.lzm.ydpt.Constants;
import com.lzm.ydpt.R;
import com.lzm.ydpt.entity.mall.ShopInfoBean;
import com.lzm.ydpt.module.mall.activity.ShopInfoActivity;
import com.lzm.ydpt.mvp.contract.ShopListContract;
import com.lzm.ydpt.mvp.presenterimpl.ShopListPresenterImpl;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 店铺列表
 * 搜索店铺
 */
public class ShopListFragment extends BaseFragment<ShopListPresenterImpl> implements ShopListContract.View {

    @BindView(R.id.ltp_shop_list)
    LoadingTip ltp_shop_list;
    @BindView(R.id.srl_shop_list)
    RefreshLayout srl_shop_list;
    @BindView(R.id.rv_shop_list)
    RecyclerView rv_shop_list;
    @BindView(R.id.tv_shop_list_count)
    TextView tv_shop_list_count;

    private static ShopListFragment mFragment;
    private List<ShopInfoBean> mShopList = new ArrayList<>();
    private RvCommonAdapter<ShopInfoBean> mRvAdapter = null;
    private int pageNum = 1;

    private String keyword = "";

    public static ShopListFragment newInstance() {
        mFragment = new ShopListFragment();
        Bundle bundle = new Bundle();
        mFragment.setArguments(bundle);
        return mFragment;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_shop_list;
    }

    @Override
    public ShopListPresenterImpl initPresenter() {
        return new ShopListPresenterImpl(this);
    }

    @Override
    protected void initView() {
        initRv();
        initAdapter();
        ltp_shop_list.setLoadingTip(LoadingTip.LoadStatus.loading);
        fetchShopList();
    }

    private void initRv() {
        setRefreshLayout(srl_shop_list);
        srl_shop_list.setRefreshHeader(new RefreshHeaderView(context).getHeaderStyleStaffF4());
        srl_shop_list.setOnLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNum++;
                fetchShopList();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNum = 1;
                fetchShopList();
            }
        });
    }

    private void initAdapter() {
        rv_shop_list.setLayoutManager(new LinearLayoutManager(context));
        mRvAdapter = new RvCommonAdapter<ShopInfoBean>(context, mShopList, R.layout.item_shop_list) {
            @Override
            protected void convert(RvViewHolder viewHolder, ShopInfoBean item, int position) {
                RoundedConnerImageView img_shop_pic = viewHolder.getView(R.id.img_shop_pic);
                ImageLoaderUtils.display(img_shop_pic, item.getImage_src());
                viewHolder.setText(R.id.tv_shop_name, item.getStore_name());
                viewHolder.setText(R.id.tv_shop_sales, "已售380");
                viewHolder.setText(R.id.tv_shop_distance, "距离" + item.getDistance() + "km");
                viewHolder.getConvertView().setOnClickListener(new OnNoDoubleClickListener() {
                    @Override
                    protected void onNoDoubleClick(View v) {
                        Bundle bundle = new Bundle();
                        bundle.putLong(Constants.INP_SHOP_ID, item.getStore_id());
                        openActivity(ShopInfoActivity.class, bundle);
                    }
                });
            }
        };
        rv_shop_list.setAdapter(mRvAdapter);
    }

    private void fetchShopList() {
        FormBodys.Builder formBodys = new FormBodys.Builder();
        formBodys.add("id", 0);
        if (!TextUtils.isEmpty(keyword)) {
            formBodys.add("keyword", keyword);
        }
        formBodys.add("page", pageNum);
        formBodys.add("per_page", 10);
        mPresenter.onFetchShopList(formBodys.build());
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
        this.pageNum = 1;
        startProgressDialog();
        fetchShopList();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mFragment = null;
    }

    @Override
    public void showMessage(String message, String method) {
        stopProgressDialog();
        showShortToast(message);
    }

    @Override
    public void showErrorView(String msg) {
        stopProgressDialog();
        showErrorView(msg, ltp_shop_list);
        ltp_shop_list.setOnReloadListener(new LoadingTip.onReloadListener() {
            @Override
            public void reload(View view) {
                ltp_shop_list.setLoadingTip(LoadingTip.LoadStatus.loading);
                fetchShopList();
            }
        });
    }

    @Override
    public void onFetchShopList(ListPageBean<ShopInfoBean> shopInfoList) {
        stopProgressDialog();
        if (pageNum == 1) {
            mShopList.clear();
            srl_shop_list.finishRefresh();
        } else {
            srl_shop_list.finishLoadMore();
        }
        mShopList.addAll(shopInfoList.getList());
        mRvAdapter.notifyDataSetChanged();
        srl_shop_list.setEnableLoadMore(shopInfoList.getList() != null && shopInfoList.getList().size() >= 10);
        ltp_shop_list.setLoadingTip(mShopList.size() > 0 ? LoadingTip.LoadStatus.finish : LoadingTip.LoadStatus.empty);
        tv_shop_list_count.setText("为您找到相关结果约" + shopInfoList.getTotal() + "个");
        tv_shop_list_count.setVisibility(View.VISIBLE);
    }
}